package a06_栈与队列;

import java.util.Stack;

/**
 * <p>
 * a05_删除字符串中的所有相邻重复项复习2
 * </p>
 *
 * @author flyduck
 * @since 2024/12/27
 */
public class a05_删除字符串中的所有相邻重复项复习2 {


    public static void main(String[] args) {
        String abbaca = removeDuplicates("abbaca");
        System.out.println();
    }

    public static String removeDuplicates(String s) {
        Stack<Character> stack = new Stack<>();

        for (char c : s.toCharArray()) {
            if (!stack.isEmpty() && stack.peek() == c) {
                stack.pop();
            }else {
                stack.push(c);
            }
        }

        char[] chars = new char[stack.size()];
        int idx = chars.length - 1;
        while (!stack.isEmpty()) {
            chars[idx--] = stack.pop();
        }
        return new String(chars);
    }
}
